{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "os.environ['CUDA_VISIBLE_DEVICES'] = ''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from tqdm import tqdm\n",
    "import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "import malaya"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# model = malaya.dependency.transformer(model = 'xlnet', quantized = True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "sendi_list = [\n",
    "    'akan',\n",
    "    'kepada',\n",
    "    'terhadap',\n",
    "    'bagi',\n",
    "    'untuk',\n",
    "    'dari',\n",
    "    'daripada',\n",
    "    'di',\n",
    "    'dengan',\n",
    "    'hingga',\n",
    "    'sampai',\n",
    "    'ke',\n",
    "    'kepada',\n",
    "    'oleh',\n",
    "    'pada',\n",
    "    'sejak',\n",
    "    'seperti',\n",
    "    'umpama',\n",
    "    'bak',\n",
    "    'tentang',\n",
    "    'laksanabagai',\n",
    "    'semenjak',\n",
    "    'dalam',\n",
    "    'antara',\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "set_sendi_list = set(sendi_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2037249"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "with open('filtered-dumping-wiki.txt') as fopen:\n",
    "    data = list(filter(None, fopen.read().split('\\n')))\n",
    "    \n",
    "data = [i for i in data if len(i) >= 2]\n",
    "\n",
    "len(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "tokenizer = malaya.preprocessing.TOKENIZER().tokenize"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "import copy\n",
    "import re\n",
    "from malaya.text.regex import _expressions\n",
    "\n",
    "def reset_t(tokens):\n",
    "    t = []\n",
    "    for i in range(len(tokens)):\n",
    "        t.append([tokens[i], 2])\n",
    "    return t\n",
    "\n",
    "# [ter][kata sifat] -> [ter][kata sifat] [end_penguat_list]\n",
    "def augment_9_0(t, row):\n",
    "    text, tokens, tokens_lower, sendi = row\n",
    "    for word in sendi:\n",
    "        i = tokens_lower.index(word)\n",
    "        negate = list(set_sendi_list - {word})\n",
    "        choice = random.choice(negate)\n",
    "        t[i][0] = choice\n",
    "        t[i][1] = 9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 1/1 [00:00<00:00, 1521.88it/s]\n"
     ]
    }
   ],
   "source": [
    "results = []\n",
    "for text in tqdm(['aku benci terhadap engkau']):\n",
    "    tokens = tokenizer(text)\n",
    "    t = reset_t(tokens)\n",
    "    t_ = copy.deepcopy(t)\n",
    "    tokens_lower = tokenizer(text.lower())\n",
    "    set_tokens = set(tokens_lower)\n",
    "    r_sendi_list = set_tokens & set_sendi_list\n",
    "    r = (t, tokens, tokens_lower, r_sendi_list)\n",
    "    augment_9_0(t_, r)\n",
    "    results.append((t, t_))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[([['aku', 2], ['benci', 2], ['terhadap', 2], ['engkau', 2]],\n",
       "  [['aku', 2], ['benci', 2], ['tentang', 9], ['engkau', 2]])]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
